package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.constants.SysConstants;
import com.wit.witmg.entity.TEATestdata;
import com.wit.witmg.entity.TEAdata;
import com.wit.witmg.entity.TECdata;
import com.wit.witmg.mapper.TEAdataMapper;
import com.wit.witmg.service.ITEATestdataService;
import com.wit.witmg.service.ITEAdataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.service.ITLogService;
import com.wit.witmg.util.DateUtil;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import com.wit.witmg.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-03-26
 */
@Service
@RequiredArgsConstructor
public class TEAdataServiceImpl extends ServiceImpl<TEAdataMapper, TEAdata> implements ITEAdataService {
    final ITLogService logService;
    final ITEATestdataService testdataService;
    @Override
    @Transactional(rollbackFor = {java.lang.Exception.class})
    public Object add(HttpServletRequest request,String sn,String table, String json) {
        if(sn!=null && !sn.equals(SysConstants.SN)){
            return JsonResult.error(ErrorCode.ERR_406);
        }
        if(table==null){
            return JsonResult.error(ErrorCode.ERR_200);
        }
//        logService.log(1,table+"+++"+json);
        try {
            String fileName= DateUtil.getCurDateTime()+"_teadata_add.txt";
            fileName=fileName.replace(":","_");
            String filePath = StringUtils.instance.getLogDir() +fileName;
            Files.write(Paths.get(filePath), json.getBytes());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(table.equals("ATTestData")){
            List<TEATestdata> list = JSONObject.parseArray(json, TEATestdata.class);
            if (list != null) {
                boolean bl = testdataService.saveBatch(list);
                if (bl) {
                    return JsonResult.success("上传成功");
                }
            }
        }else if(table.equals("ATtestNumTable")) {
            List<TEAdata> list = JSONObject.parseArray(json, TEAdata.class);
            if (list != null) {
                for (TEAdata t : list) {
                   saveOrUpdate( t, new QueryWrapper<TEAdata>().eq("id", t.getId()));
                }
                return JsonResult.success("上传成功");
            }
        }else{
            return JsonResult.error(ErrorCode.ERR_200);
        }
        return JsonResult.error(ErrorCode.ERR_504);
    }


    @Override
    public Object get(HttpServletRequest request,String ph) {
        if(ph==null || ph.isEmpty()){
            return JsonResult.error(ErrorCode.ERR_200);
        }
        QueryWrapper<TEAdata> qw=new QueryWrapper<>();
        qw.eq("PiHao",ph);
        List<TEAdata> list = list(qw);
        if(list == null || list.isEmpty()){
            return JsonResult.error(ErrorCode.ERR_313);
        }
        JSONObject obj= StringUtils.instance.getBaseResultObj();
        obj.put("message",list);
        obj.put("list",testdataService.list(new QueryWrapper<TEATestdata>().eq("TestBianHao",ph)));
        return obj;
    }
}
